package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.Group;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ServerEntity;

/**
 * Created by lijiateng on 2019/6/16.
 */

public class ADA_DialogChooseServerList extends RecyclerView.Adapter<ADA_DialogChooseServerList.MyViewHolder> {

    private Context mContext;
    private List<ServerEntity> mData;

    private String defaultServerId;
    private boolean isDoubleCheck = false;

    public ADA_DialogChooseServerList(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ADA_DialogChooseServerList.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_choose_server, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final ADA_DialogChooseServerList.MyViewHolder holder, int position) {
        final ServerEntity serverEntity = mData.get(position);
        holder.tvServerName.setText(StringUtils.getUserName(serverEntity.getTechnicianName()));
        holder.tvServerTitle.setText(StringUtils.isEmpty(serverEntity.getTechnicianLabel()) ? "技师" : serverEntity.getTechnicianLabel());
        holder.rbChoose.setClickable(false);
        if (!TextUtils.isEmpty(defaultServerId) && defaultServerId.equals(serverEntity.getTechnicianId())) {
            holder.rbChoose.setChecked(true);
            holder.rbChoose.setEnabled(true);
        } else {
            holder.rbChoose.setChecked(false);
            holder.rbChoose.setEnabled(false);
        }
        holder.cover.setVisibility(!serverEntity.isEnable() ? View.VISIBLE : View.GONE);
        holder.itemView.setEnabled(serverEntity.isEnable());

        if (isDoubleCheck) {
            holder.rbChoose.setButtonDrawable(R.drawable.selector_schedule_server_choose);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    serverEntity.setChecked(!serverEntity.isChecked());
                    holder.rbChoose.setChecked(serverEntity.isChecked());
                }
            });
        } else {
            holder.rbChoose.setButtonDrawable(R.drawable.selector_bg_radio_button_choose);
        }

    }

    public List<ServerEntity> getData() {
        return mData;
    }

    public List<ServerEntity> getSelectedServerList() {
        List<ServerEntity> selectServer = new ArrayList<>();
        if (!ListUtil.isListEmpty(mData)) {
            for (ServerEntity entity : mData) {
                if (entity.isChecked()) {
                    selectServer.add(entity);
                }
            }
        }
        return selectServer;
    }

    public void setData(List<ServerEntity> data) {
        this.mData = data;
    }

    public void setDoubleCheck(boolean doubleCheck) {
        isDoubleCheck = doubleCheck;
    }


    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    public void setDefaultServerId(String defaultServerId) {
        this.defaultServerId = defaultServerId;
    }

    public void clearSelect() {
        if (!ListUtil.isListEmpty(mData)) {
            for (ServerEntity entity : mData) {
                entity.setChecked(false);
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvServerName;
        @BindView(R.id.tv_server_title)
        TextView tvServerTitle;
        @BindView(R.id.rb_choose)
        RadioButton rbChoose;
        @BindView(R.id.group_cover)
        Group cover;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
